package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.qo.EmployeeQueryObject;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Permission> pageResult = permissionService.query(qo);
        model.addAttribute("result", pageResult);
        return "/permission/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            permissionService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败");
        }
        return new JsonResult(true, "操作成功");
    }

    //处理加载权限请求
    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload() {

        try {
            permissionService.reload();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "加载失败");
        }
        return new JsonResult(false, "加载成功");








        /*try {
            //调用业务方法
            permissionService.reload();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "加载失败");
        }
        return new JsonResult(true, "加载成功");*/
    }
}
